package com.android.smmi.base;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.android.smmi.R;
import com.android.smmi.util.ProgressDialogUtil;

public abstract class BaseActivity extends Activity implements OnClickListener
{
	public ProgressDialogUtil	pdu;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		if (pdu == null)
			pdu = new ProgressDialogUtil(BaseActivity.this);
	}

	public void onAttachedToWindow()
	{
		super.onAttachedToWindow();
	}

	protected void onPause()
	{
		super.onPause();
		System.gc();
	}

	protected void onResume()
	{
		super.onResume();
	}

	protected void onStop()
	{
		System.gc();
		super.onStop();
	}

	protected void showProcess(String message)
	{
		pdu.show(ProgressDialog.STYLE_SPINNER, null, message, R.drawable.icon_more, true);
	}

	protected void cancleProcess()
	{
		pdu.cancel();
	}

	protected void showToast(int paramInt)
	{
		Toast.makeText(this, paramInt, 0).show();
	}

	protected void showToast(String message)
	{

		DisplayMetrics localDisplayMetrics = new DisplayMetrics();
		BaseActivity.this.getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics);

		if (localDisplayMetrics.heightPixels < 800)
		{
			LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View layout = inflater.inflate(R.layout.toast, (ViewGroup) findViewById(R.id.toast_layout));

			TextView text = (TextView) layout.findViewById(R.id.info_toast);
			text.setText(message);

			Toast toast = new Toast(this);
			toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, 50);
			toast.setDuration(Toast.LENGTH_SHORT);
			toast.setView(layout);
			toast.show();
		}
		else
		{
			LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View layout = inflater.inflate(R.layout.toast, (ViewGroup) findViewById(R.id.toast_layout));

			TextView text = (TextView) layout.findViewById(R.id.info_toast);
			text.setText(message);

			Toast toast = new Toast(this);
			toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP, 0, 80);
			toast.setDuration(Toast.LENGTH_SHORT);
			toast.setView(layout);
			toast.show();
		}
	}
}
